package cn.dsttl3.tupan;

import cn.dsttl3.tupan.bean.baidu.FileInfo;
import cn.dsttl3.tupan.utils.SizeUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import java.util.List;
public class SampleItemProvider extends BaseItemProvider {

    private List<FileInfo> list;
    private AbilitySlice slice;
    public SampleItemProvider(List<FileInfo> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()){
            return list.get(position);
        }
        return null;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_view, null, false);
        } else {
            cpt = convertComponent;
        }
        FileInfo fileInfo = list.get(position);
        Text fname = (Text) cpt.findComponentById(ResourceTable.Id_file_name);
        Text finfo = (Text) cpt.findComponentById(ResourceTable.Id_file_info);
        Image image = (Image) cpt.findComponentById(ResourceTable.Id_file_type);
        fname.setText(fileInfo.getServer_filename());
        if (fileInfo.getIsdir() == 0){
            finfo.setText(fileInfo.getPath() + "\t" + new SizeUtil().getPrintSize(fileInfo.getSize()));
            switch (fileInfo.getCategory()){
                case 1:
                    image.setPixelMap(ResourceTable.Media_f1);
                    break;
                case 2:
                    image.setPixelMap(ResourceTable.Media_f2);
                    break;
                case 3:
                    image.setPixelMap(ResourceTable.Media_f3);
                    break;
                case 4:
                    image.setPixelMap(ResourceTable.Media_f4);
                    break;
                case 5:
                    image.setPixelMap(ResourceTable.Media_f5);
                    break;
                case 6:
                    if(fileInfo.getServer_filename().endsWith(".rar")||fileInfo.getServer_filename().endsWith(".zip")||fileInfo.getServer_filename().endsWith(".7z")){
                        image.setPixelMap(ResourceTable.Media_f9);
                    }else {
                        image.setPixelMap(ResourceTable.Media_f6);
                    }
                    break;
                case 7:
                    image.setPixelMap(ResourceTable.Media_f7);

            }
        }else {
            finfo.setText(fileInfo.getPath());
            image.setPixelMap(ResourceTable.Media_f0);
        }

        return cpt;
    }



}